package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.L1Object;
import com.lineage.server.serverpackets.S_NpcChatPacket;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.utils.SQLUtil;
import com.lineage.server.world.World;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NpcTalk10 extends TimerTask {
  private static final Logger log = Logger.getLogger(NpcTalk10.class.getName());
  
  private static final ConcurrentHashMap<Integer, ArrayList<Chat>> _chatList = new ConcurrentHashMap<>();
  
  private static int index;
  
  private static NpcTalk10 _instance;
  
  public static NpcTalk10 get() {
    if (_instance == null)
      _instance = new NpcTalk10(); 
    return _instance;
  }
  
  public static void load() {
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM npc_talk");
      rs = pstm.executeQuery();
      while (rs.next()) {
        int npcid = rs.getInt("npc_id");
        Chat chat = new Chat();
        chat.chatType = rs.getByte("shout");
        chat.chatContent = rs.getString("talk");
        chat.chatTime = rs.getInt("sec") * 1000;
        if (_chatList.containsKey(Integer.valueOf(npcid))) {
          ((ArrayList<Chat>)_chatList.get(Integer.valueOf(npcid))).add(chat);
          continue;
        } 
        ArrayList<Chat> fastTable = new ArrayList<>();
        fastTable.add(chat);
        _chatList.put(Integer.valueOf(npcid), fastTable);
      } 
    } catch (SQLException e) {
      log.log(Level.SEVERE, e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs, pstm, con);
    } 
    GeneralThreadPool.get().scheduleAtFixedRate(new NpcTalk10(), 0L, 1000L);
  }
  
  public static final void nextIndex() {
    index++;
  }
  
  public static final void startDummyChat(L1NpcInstance npc) {
    ArrayList<Chat> chatTable = _chatList.get(Integer.valueOf(npc.getNpcId()));
    if (chatTable == null)
      return; 
    Chat c = chatTable.get(index % chatTable.size());
    long currentTime = System.currentTimeMillis();
    if (currentTime > c.lastTime) {
      c.lastTime = currentTime + c.chatTime;
      String content = c.chatContent;
      byte type = c.chatType;
      npc.broadcastPacketAll((ServerBasePacket)new S_NpcChatPacket(npc, content, type));
    } 
  }
  
  public final void run() {
    Iterator<L1Object> iterator = World.get().getAllVisibleObjects().values().iterator();
    while (iterator.hasNext()) {
      L1Object object = iterator.next();
      if (object instanceof L1NpcInstance)
        startDummyChat((L1NpcInstance)object); 
    } 
  }
  
  private static class Chat {
    private byte chatType;
    
    private String chatContent;
    
    private int chatTime;
    
    private long lastTime;
    
    private Chat() {}
  }
}
